﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using Framework.Graphics;
using Framework.Graphics.Light;

using Framework.GameStateManagment;

namespace Framework.Map
{
    public class TileMap
    {
        private Int32 _mapWidth;
        private Int32 _mapHeight;

        private Texture2D[] _textures;
        private Texture2D[] _normals;
        private Texture2D[] _shadows;
        private Texture2D[] _walltextures;

        private Tile[,] _allTiles;

        public int MapHeightPx
        {
            get { return _mapHeight * 50; }
        }

        public int MapWidthPx
        {
            get { return _mapWidth * 50; }
        }

        public Tile[,] AllTiles
        {
            get { return _allTiles; }
            set { _allTiles = value; }
        }

        public Texture2D[] Shadows
        {
            get { return _walltextures; }
            set { _walltextures = value; }
        }
        
        public void Init(ContentManager content, string mapname)
        {
            #region Prepare for Loading the map
            string fullPath = @"Content/Levels/" + mapname + ".txt";
            string contentPath = @"Levels/Graphics/";

            StreamReader SR = new StreamReader(fullPath);

            SR.ReadLine();
            SR.ReadLine();
            SR.ReadLine();

            #endregion

            #region Load the Textures
            List<Texture2D> temp = new List<Texture2D>();
            while (true)
            {
                string t;
                t = SR.ReadLine();
                if (t != "-----------------------------------------------")
                {
                    temp.Add(content.Load<Texture2D>(contentPath + @"Texture/" + t));
                }
                else
                {
                    _textures = temp.ToArray();
                    //SR.ReadLine();
                    //SR.ReadLine();
                    break;
                }
            }

            //SR.ReadLine();
            SR.ReadLine();
            SR.ReadLine();
            #endregion

            #region Load the Normals
            temp = new List<Texture2D>();
            while (true)
            {
                string t;
                t = SR.ReadLine();
                if (t != "-----------------------------------------------")
                {
                    temp.Add(content.Load<Texture2D>(contentPath + @"Normal/" + t));
                }
                else
                {
                    _normals = temp.ToArray();

                    break;
                }
            }

            SR.ReadLine();
            SR.ReadLine();
            //SR.ReadLine();
            #endregion

            #region Load the Shadows <-- not in use
            //temp = new List<Texture2D>();
            //while (true)
            //{
            //    string t;
            //    t = SR.ReadLine();
            //    if (t != "-----------------------------------------------")
            //    {
            //        temp.Add(content.Load<Texture2D>(contentPath + @"Shadow/" + t));
            //    }
            //    else
            //    {
            //        _shadows = temp.ToArray();
            //        break;
            //    }
            //}


            //SR.ReadLine();
            //SR.ReadLine();
            //SR.ReadLine();
            #endregion

            #region Load Width and Height

            _mapWidth = Convert.ToInt32(SR.ReadLine());
            _mapHeight = Convert.ToInt32(SR.ReadLine());

            _allTiles = new Tile[_mapWidth, _mapHeight];

            SR.ReadLine();
            SR.ReadLine();
            SR.ReadLine();

            #endregion

            #region Load the Wall Textures
            temp = new List<Texture2D>();
            List<Texture2D> temp2 = new List<Texture2D>(); // 10.05.2012
            while (true)
            {
                string t;
                t = SR.ReadLine();
                if (t != "-----------------------------------------------")
                {
                    temp.Add(content.Load<Texture2D>(contentPath + @"Wall/" + t));
                    temp2.Add(content.Load<Texture2D>(contentPath + @"Shadow/" + t)); // 10.05.2012
                }
                else
                {
                    _walltextures = temp.ToArray();
                    _shadows = temp2.ToArray();
                    //SR.ReadLine();
                    //SR.ReadLine();
                    break;
                }
            }

            SR.ReadLine();
            SR.ReadLine();
            //SR.ReadLine();
            #endregion

            #region Load the Map

            #region Texture
            for (Int32 y = 0; y < _mapHeight; y++)
            {
                string[] tempstr = SR.ReadLine().Split(new char[] { ',', });
                for (Int32 x = 0; x < _mapWidth; x++)
                {
                    _allTiles[x, y] = new Tile(x, y);
                    _allTiles[x, y].Texture = Convert.ToInt32(tempstr[x]);
                }
            }

            SR.ReadLine();
            SR.ReadLine();
            SR.ReadLine();
            #endregion

            #region Enterable

            for (Int32 y = 0; y < _mapHeight; y++)
            {
                string[] tempstr = SR.ReadLine().Split(new char[] { ',', });
                for (Int32 x = 0; x < _mapWidth; x++)
                {
                    if (tempstr[x] == "01")
                    {
                        _allTiles[x, y].TileEnterable = true;
                    }
                    else
                    {
                        _allTiles[x, y].TileEnterable = false;
                    }
                }
            }

            SR.ReadLine();
            SR.ReadLine();
            SR.ReadLine();
            #endregion

            #region WallTexture

            for (Int32 y = 0; y < _mapHeight; y++)
            {
                string[] tempstr = SR.ReadLine().Split(new char[] { ',', });
                for (Int32 x = 0; x < _mapWidth; x++)
                {
                    _allTiles[x, y].WallTexture = Convert.ToInt32(tempstr[x]);
                    _allTiles[x, y].Shadow = Convert.ToInt32(tempstr[x]);
                }
            }
            #endregion

            for (int i = 0; i < 8; i++)
            {
                SR.ReadLine();
            }

            #endregion

        }

        public void DrawAll(LightManager LM)
        {
            for (int y = 0; y < _mapHeight; y++)
            {
                for (int x = 0; x < _mapWidth; x++)
                {
                    LM.AddBumpColor(_textures[_allTiles[x,y].Texture], new Vector2(x * 50, y * 50),0.5f,Color.White);
                    //Camera.TryDraw(_textures[_allTiles[x,y].Texture], new Vector2(x * 50, y * 50), Color.White);
                }
            }

            int fsdf = 0;
        }

        public void DrawNormal(LightManager LM)
        {
            for (int y = 0; y < _mapHeight; y++)
            {
                for (int x = 0; x < _mapWidth; x++)
                {
                    LM.AddBumpNormal(_normals[_allTiles[x, y].Texture], new Vector2(x * 50, y * 50), 0.5f, Color.White);
                    //Camera.TryDraw(_textures[_allTiles[x,y].Texture], new Vector2(x * 50, y * 50), Color.White);
                }
            }
        }

        public void DrawShadowObjects(LightManager LM)
        {
            for (int y = 0; y < _mapHeight; y++)
            {
                for (int x = 0; x < _mapWidth; x++)
                {
                    if (_allTiles[x, y].TileEnterable == false)
                    {
                        LM.AddShadowObjects(_shadows[_allTiles[x, y].Shadow], new Vector2(x * 50, y * 50), 0.5f, Color.White);
                        //Camera.TryDraw(_textures[_allTiles[x,y].Texture], new Vector2(x * 50, y * 50), Color.White);
                    }
                }
            }
        }

        public void DrawAll()
        {
            for (int y = 0; y < _mapHeight; y++)
            {
                for (int x = 0; x < _mapWidth; x++)
                {
                    Camera.SpriteBatch.Draw(_textures[_allTiles[x, y].Texture], new Vector2(x * 50, y * 50)+
                        Input.AllMovement,null, Color.White,0f,Vector2.Zero,0.5f,SpriteEffects.None,0f);
                }
            }

        }

        public void DrawNormal()
        {
            for (int y = 0; y < _mapHeight; y++)
            {
                for (int x = 0; x < _mapWidth; x++)
                {
                    Camera.SpriteBatch.Draw(_normals[_allTiles[x, y].Texture], new Vector2(x * 50, y * 50)+
                        Input.AllMovement, null, Color.White, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);
                }
            }
        }
    }
}
